package com.java110.mall.goods.bmo.allocationOrder.impl;

import com.java110.core.annotation.Java110Transactional;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.mall.goods.bmo.allocationOrder.IDeleteAllocationOrderBMO;
import com.java110.intf.IAllocationOrderInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.account.AllocationOrderPo;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("deleteAllocationOrderBMOImpl")
public class DeleteAllocationOrderBMOImpl implements IDeleteAllocationOrderBMO {

    @Autowired
    private IAllocationOrderInnerServiceSMO allocationOrderInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    /**
     * @param allocationOrderPo 数据
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> delete(AllocationOrderPo allocationOrderPo) {
        ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setProductId(allocationOrderPo.getProductId());
        productSpecValueDto.setSpecId(allocationOrderPo.getSpecId());
        List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);
        Assert.listOnlyOne(productSpecValueDtos, "查询规格值表错误");
        //获取库存
        String stock = productSpecValueDtos.get(0).getStock();
        //获取调拨数量
        String productCount = allocationOrderPo.getProductCount();
        //计算总库存
        double count = Double.parseDouble(stock) + Double.parseDouble(productCount);
        int flag = allocationOrderInnerServiceSMOImpl.deleteAllocationOrder(allocationOrderPo);

        if (flag > 0) {
            ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
            productSpecValuePo.setValueId(productSpecValueDtos.get(0).getValueId());
            productSpecValuePo.setStock(String.valueOf(count));
            productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
